package data;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class AuthorMap {
	private Connection conn;
	
	public AuthorMap() throws Exception{		
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String url = "jdbc:mysql://localhost:3306/" + "bibsonomy";
		String dbuser = "root";
		String dbpass = "123456";
		Connection con = (Connection) DriverManager.getConnection(url, dbuser,
				dbpass);
		this.conn = con;
	}
	
	public void extractAuthorMap() throws Exception{
		Statement stmt = null;
		ResultSet rs = null;
		String path = "D:\\CityU\\project\\bibsonomy\\data" + "\\authorMap.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(path));
		HashMap<String, Long> map = new HashMap<String,Long>();
		
		try{
			stmt = (Statement) conn.createStatement();
			rs = (ResultSet) stmt
					.executeQuery("select author from bibtex where (entrytype = 'article' or 'inproceedings') and author is not null and author != '' and bibtexAbstract is not null");
			long cnt = 1;
			while(rs.next()) {
				String au = rs.getString("author");
				au = au.trim();
				
				if(au.indexOf(" and ") == -1){
					String str = au.trim();					
					str = str.replaceAll("\t", " ");
					str = str.replaceAll("\n", " ");
					str = str.replaceAll("\r", " ");
					str = str.trim();
					
					char[] chArr = str.toCharArray();
					str = "";
					for(char ch : chArr) {
						String c = transfer(ch);						
						str += c;
					}
                    str = str.trim();
					
					if(str.equalsIgnoreCase("") || str.equalsIgnoreCase("."))
						continue;
					if(map.containsKey(str))
						continue;
					cnt++;
					map.put(str, cnt);
					continue;
				}
				
				String[] tokens = au.split(" and ");				
				for(String str : tokens) {
					str = str.trim();
					str = str.replaceAll("\t", " ");
					str = str.replaceAll("\n", " ");
					str = str.replaceAll("\r", " ");
					str = str.trim();
					
					char[] chArr = str.toCharArray();
					str = "";
					for(char ch : chArr) {
						String c = transfer(ch);						
						str += c;
					}
                    str = str.trim();
                   
					if(str.equalsIgnoreCase("") || str.equalsIgnoreCase("."))
						continue;
					if(map.containsKey(str))
						continue;
					cnt++;
					map.put(str, cnt);
				}
			}
		    
			for(Map.Entry<String,Long> entry : map.entrySet()) {
				String author = entry.getKey();
				long id = entry.getValue();
				writer.write(author + "\t" + id);
				writer.newLine();
			}
			
			
		} finally{
			writer.flush();
			writer.close();
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException sqlEx) {
				} // ignore
				rs = null;
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException sqlEx) {
				} // ignore
				stmt = null;
			}
			this.conn.close();
			
		}
		
	}
	
	private String transfer(char ch) {

		String c = "";
		if (ch >= 65 && ch <= 90) {
			c = Character.toString(ch);
		} else if (ch >= 97 && ch <= 122) {
			c = Character.toString(ch);
		}

		else if (Character.isWhitespace(ch)) {
			c = Character.toString(ch);
		} else {
			c = "_";
		}
		return c;
	}   	
	
	public static void main(String[] args) throws Exception{
/*		
		String au = "Tore DybÃƒÂ¥";
		String[] tokens = au.split(" and ");		
		for(String str : tokens) {
			str = str.trim();
			str = str.replaceAll("\t", " ");
			str = str.replaceAll("\n", " ");
			str = str.replaceAll("\r", " ");
			str = str.trim();
			char[] chArr = str.toCharArray();
			str = "";
			for(char ch : chArr) {
				String c = "";
				if(Character.isLetter(ch)){
					c = Character.toString(ch);
				} else if(Character.isWhitespace(ch)){
					c = Character.toString(ch);
				}
				else{
					c = "_";
				}
				str += c;
			}
			System.out.println(str);
		}
	*/
		
		AuthorMap app = new AuthorMap();
		app.extractAuthorMap();
		
		
	}

}
